using System;
using UnityEngine;

public class SoundEmitter : MonoBehaviour
{
	public AudioClip sound;

	public AudioClip soundRoll;

	public bool playOnCollision = true;

	public bool playOnAngularVelocityChange;

	public bool playRollSound;

	public float soundScale = 1f;

	private AudioSource soundSource;

	private AudioSource soundSourceRoll;

	private float soundRollVolume;

	private float soundRollPitch = 1f;

	private bool freezed;

	private int contacts;

	private Vector3 prevVelocity = Vector3.zero;

	private Vector3 prevAngularVelocity = Vector3.zero;

	private void Awake()
	{
		soundSource = base.gameObject.AddComponent<AudioSource>();
		soundSourceRoll = base.gameObject.AddComponent<AudioSource>();
		soundSource.clip = sound;
		soundSource.loop = false;
		soundSource.rolloffMode = AudioRolloffMode.Linear;
		soundSource.maxDistance = 30f;
		soundSource.dopplerLevel = 0.1f;
		soundSourceRoll.rolloffMode = AudioRolloffMode.Linear;
		soundSourceRoll.maxDistance = 30f;
		soundSourceRoll.dopplerLevel = 0.1f;
		soundSourceRoll.clip = soundRoll;
		soundSourceRoll.volume = 0f;
		soundSourceRoll.loop = true;
		if (playRollSound)
		{
			Audio.SetRollingSoundMode(RollingMode.previousMode, soundSourceRoll);
		}
		EventManager.GameFreezeEvent += OnGameFreeze;
		EventManager.GameUnfreezeEvent += OnGameUnfreeze;
	}

	private void OnDestroy()
	{
		EventManager.GameFreezeEvent -= OnGameFreeze;
		EventManager.GameUnfreezeEvent -= OnGameUnfreeze;
	}

	private void Update()
	{
		if (freezed || !playRollSound)
		{
			return;
		}
		if (Audio.isEnabledSound)
		{
			if (!soundSourceRoll.isPlaying)
			{
				soundSourceRoll.Play();
			}
			float volume = Mathf.Lerp(soundSourceRoll.volume, soundRollVolume, Time.deltaTime * 10f);
			float pitch = Mathf.Lerp(soundSourceRoll.pitch, soundRollPitch, Time.deltaTime * 1f);
			Audio.SetRollingSoundValues(volume, pitch, soundSourceRoll);
		}
		else if (soundSourceRoll.isPlaying)
		{
			soundSourceRoll.Stop();
		}
	}

	private void OnGameFreeze()
	{
		freezed = true;
		if (playRollSound)
		{
			soundRollVolume = 0f;
			soundSourceRoll.volume = 0f;
		}
	}

	private void OnGameUnfreeze()
	{
		freezed = false;
	}

	private void OnCollisionEnter()
	{
		contacts++;
		if (playOnCollision)
		{
			float num = (base.GetComponent<Rigidbody>().velocity - prevVelocity).magnitude * 0.02f;
			num *= soundScale;
			if (soundSource == null)
			{
				Debug.Log("WTF: SoundEmitter soundSource = null");
			}
			else
			{
				Audio.PlayShotSound(sound, num, soundSource);
			}
		}
	}

	private void OnCollisionExit()
	{
		contacts--;
	}

	private void FixedUpdate()
	{
		if (freezed || base.GetComponent<Rigidbody>() == null)
		{
			return;
		}
		if (playOnAngularVelocityChange)
		{
			float magnitude = (base.GetComponent<Rigidbody>().angularVelocity - prevAngularVelocity).magnitude;
			if (magnitude > 0.8f)
			{
				float magnitude2 = (base.GetComponent<Rigidbody>().velocity - prevVelocity).magnitude;
				float num = magnitude2 * 0.02f * (float)Math.PI;
				num *= soundScale;
				Audio.PlayShotSound(sound, num, soundSource);
			}
		}
		if (playRollSound)
		{
			SetRollSound();
		}
		prevVelocity = base.GetComponent<Rigidbody>().velocity;
		prevAngularVelocity = base.GetComponent<Rigidbody>().angularVelocity;
	}

	private void SetRollSound()
	{
		float num = 1f;
		if (contacts > 0)
		{
			soundRollVolume = base.GetComponent<Rigidbody>().velocity.sqrMagnitude / 400f;
			soundRollVolume = Mathf.Clamp(soundRollVolume, 0f, 1f);
			soundRollPitch = Mathf.Lerp(0.8f, 1f, base.GetComponent<Rigidbody>().velocity.magnitude / 10f);
		}
		else
		{
			soundRollVolume = 0f;
		}
		soundRollVolume *= num;
	}
}
